<?php
namespace app\api\controller;

use app\admin\model\exitqun\Exitqun;
use app\admin\model\groupchat\Groupchat;
use app\admin\model\groupchateight\Groupchateight;
use app\admin\model\groupchatfive\Groupchatfive;
use app\admin\model\groupchatfour\Groupchatfour;
use app\admin\model\groupchatnine\Groupchatnine;
use app\admin\model\groupchatseven\Groupchatseven;
use app\admin\model\groupchatsix\Groupchatsix;
use app\admin\model\groupchatten\Groupchatten;
use app\admin\model\groupchatthree\Groupchatthree;
use app\admin\model\groupchattwo\Groupchattwo;
use app\admin\model\groupchatyuangong\Groupchatyuangong;
use app\admin\model\tangonggao\Tangonggao;
use app\common\controller\Api;

class Qun extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function list(){
        $qunModel = new \app\admin\model\qun\Qun();
        $userinfo = $this->auth->getUserinfo();
        $exitqunModel = new Exitqun();
        $list = collection($qunModel->where([])->order('id asc')->select())->toArray();
        $newlist = [];
        foreach ($list as $k=>$v){
            $newlist[$k]['id'] = $v['id'];
            $newlist[$k]['name'] = $v['name'];
            $newlist[$k]['img'] = 'http://47.239.156.92'.$v['img'];
            $lastinfo = $exitqunModel->where(['user_id'=>$userinfo['id'],'qun_id'=>$v['id']])->find();
            if($lastinfo){
                $lastinfo = $lastinfo->toArray();
                if($v['id']==1){
                    $groupchatModel = new Groupchat();
                }elseif ($v['id']==2){
                    $groupchatModel = new Groupchattwo();
                }elseif ($v['id']==3){
                    $groupchatModel = new Groupchatthree();
                }elseif ($v['id']==4){
                    $groupchatModel = new Groupchatfour();
                }elseif ($v['id']==5){
                    $groupchatModel = new Groupchatfive();
                }elseif ($v['id']==6){
                    $groupchatModel = new Groupchatsix();
                }elseif ($v['id']==7){
                    $groupchatModel = new Groupchatseven();
                }elseif ($v['id']==8){
                    $groupchatModel = new Groupchateight();
                }elseif ($v['id']==9){
                    $groupchatModel = new Groupchatnine();
                }elseif ($v['id']==10){
                    $groupchatModel = new Groupchatten();
                }elseif($v['id']==11){
                    $groupchatModel = new Groupchatyuangong();
                }
                $where['create_time'] = ['>',$lastinfo['create_time']];
                $newlist[$k]['noread_nums'] = $groupchatModel->where($where)->count();
            }else{
                if($v['id']==1){
                    $groupchatModel = new Groupchat();
                }elseif ($v['id']==2){
                    $groupchatModel = new Groupchattwo();
                }elseif ($v['id']==3){
                    $groupchatModel = new Groupchatthree();
                }elseif ($v['id']==4){
                    $groupchatModel = new Groupchatfour();
                }elseif ($v['id']==5){
                    $groupchatModel = new Groupchatfive();
                }elseif ($v['id']==6){
                    $groupchatModel = new Groupchatsix();
                }elseif ($v['id']==7){
                    $groupchatModel = new Groupchatseven();
                }elseif ($v['id']==8){
                    $groupchatModel = new Groupchateight();
                }elseif ($v['id']==9){
                    $groupchatModel = new Groupchatnine();
                }elseif ($v['id']==10){
                    $groupchatModel = new Groupchatten();
                }elseif($v['id']==11){
                    $groupchatModel = new Groupchatyuangong();
                }
                $newlist[$k]['noread_nums'] = $groupchatModel->where([])->count();
            }
        }
        $this->success('成功',$newlist);
    }

    public function into(){
        $qun_id = $this->request->param('qun_id');
        if($qun_id==11){
            $arr=[105,108,109,110];
            $userinfo =$this->auth->getUserinfo();
            if(!in_array($userinfo['id'],$arr)){
                $this->error('您不是公司员工不能进入此群');
            }
        }
        $userinfo =$this->auth->getUserinfo();
        $qunModel = new \app\admin\model\qun\Qun();
        $quninfo = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        if($quninfo['chengyuan']==''){
            $qunModel->where(['id'=>$qun_id])->update(['chengyuan'=>$userinfo['id']]);
        }else{
            $idarr = explode(",",$quninfo['chengyuan']);
            if(!in_array($userinfo['id'],$idarr)){
                array_push($idarr,$userinfo['id']);
            }
            $newidstr=implode(",",$idarr);
            $qunModel->where(['id'=>$qun_id])->update(['chengyuan'=>$newidstr]);
        }
        $this->success('成功');
    }

    public function goto(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $userinfo =$this->auth->getUserinfo();
        $exitqunModel = new Exitqun();
        $info = $exitqunModel->where(['user_id'=>$userinfo['id'],'qun_id'=>$qun_id])->find();
        if($info){
            $info = $info->toArray();
            $exitqunModel->where(['id'=>$info['id']])->update(['create_time'=>time()]);
        }else{
            $indata['user_id'] = $userinfo['id'];
            $indata['qun_id'] = $qun_id;
            $indata['create_time'] = time();
            $exitqunModel->insert($indata);
        }
        $this->success('退出群聊成功');
    }

    /**查看群成员**/
    public function qunchengyuan(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $qunModel = new \app\admin\model\qun\Qun();
        $userModel = new \app\admin\model\User();
        $info = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        if($info['chengyuan']!=''){
            $where['id']=['in',$info['chengyuan']];
            $userlist = collection($userModel->where($where)->select())->toArray();
            $all_chengyuan =[];
            foreach ($userlist as $k=>$v){
                $all_chengyuan[$k]['id']=$v['id'];
                $all_chengyuan[$k]['nickname']=$v['nickname'];
                if($v['avatar'] == ''){
                    $all_chengyuan[$k]['avatar']='http://47.239.156.92/avatar.png';
                }else{
                    $all_chengyuan[$k]['avatar']='http://47.239.156.92'.$v['avatar'];
                }
            }
        }else{
            $all_chengyuan =[];
        }
        $this->success('成功',$all_chengyuan);
    }

    /**群管理**/
    public function qunguanli(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $qunModel = new \app\admin\model\qun\Qun();
        $userModel = new \app\admin\model\User();
        $info = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        if($info['guanli']!=''){
            $where['id']=['in',$info['guanli']];
            $userlist = collection($userModel->where($where)->select())->toArray();
            $guanli=[];
            foreach ($userlist as $k=>$v){
                $guanli[$k]['id']=$v['id'];
                $guanli[$k]['nickname']=$v['nickname'];
                if($v['avatar'] == ''){
                    $guanli[$k]['avatar']='http://47.239.156.92/avatar.png';
                }else{
                    $guanli[$k]['avatar']='http://47.239.156.92'.$v['avatar'];
                }
            }
        }else{
            $guanli=[];
        }
        $this->success('成功',$guanli);
    }

    public function setnickname(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $nickname = $this->request->param('nickname');
        $userinfo =$this->auth->getUserinfo();
        if($qun_id==1){
            $groupModel = new Groupchat();
        }else if($qun_id==2){
            $groupModel = new Groupchattwo();
        }else if($qun_id==3){
            $groupModel = new Groupchatthree();
        }else if($qun_id==4){
            $groupModel = new Groupchatfour();
        }else if($qun_id==5){
            $groupModel = new Groupchatfive();
        }else if($qun_id==6){
            $groupModel = new Groupchatsix();
        }else if($qun_id==7){
            $groupModel = new Groupchatseven();
        }else if($qun_id==8){
            $groupModel = new Groupchateight();
        }else if($qun_id==9){
            $groupModel = new Groupchatnine();
        }else if($qun_id==10){
            $groupModel = new Groupchatten();
        }else if($qun_id==11){
            $groupModel = new Groupchatyuangong();
        }
        $groupModel->where(['user_id'=>$userinfo['id']])->update(['nickname'=>$nickname]);
        $this->success('成功');
    }

    /**查看群公告和本群昵称群头像**/
    public function look(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $userinfo =$this->auth->getUserinfo();
        if($qun_id==1){
            $groupModel = new Groupchat();
        }else if($qun_id==2){
            $groupModel = new Groupchattwo();
        }else if($qun_id==3){
            $groupModel = new Groupchatthree();
        }else if($qun_id==4){
            $groupModel = new Groupchatfour();
        }else if($qun_id==5){
            $groupModel = new Groupchatfive();
        }else if($qun_id==6){
            $groupModel = new Groupchatsix();
        }else if($qun_id==7){
            $groupModel = new Groupchatseven();
        }else if($qun_id==8){
            $groupModel = new Groupchateight();
        }else if($qun_id==9){
            $groupModel = new Groupchatnine();
        }else if($qun_id==10){
            $groupModel = new Groupchatten();
        }else if($qun_id==11){
            $groupModel = new Groupchatyuangong();
        }
        $qunModel = new \app\admin\model\qun\Qun();
        $quninfo = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        $one = $groupModel->where(['user_id'=>$userinfo['id']])->find();
        if($one){
            $one = $one->toArray();
            $this->success('成功',[
                'nickanme'=>$one['nickname'],
                'gonggao'=>$quninfo['gonggao'],
                'name'=>$quninfo['name'],
                'img'=>'http://47.239.156.92'.$quninfo['img']]);
        }else{
            $this->success('成功',[
                'nickanme'=>$userinfo['nickname'],
                'gonggao'=>$quninfo['gonggao'],
                'name'=>$quninfo['name'],
                'img'=>'http://47.239.156.92'.$quninfo['img']
                ]);
        }
    }

    /**修改群公告**/
    public function setgonggao(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $gonggao = $this->request->param('gonggao');
        $userinfo =$this->auth->getUserinfo();
        $qunModel = new \app\admin\model\qun\Qun();
        $quninfo = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        if($quninfo['guanli']==''){
            $this->error('您不是群管理不能修改公告');
        }else{
            $idarr=explode(",",$quninfo['guanli']);
            if(!in_array($userinfo['id'],$idarr)){
                $this->error('您不是群管理不能修改公告');
            }
        }
        $qunModel->where(['id'=>$qun_id])->update(['gonggao'=>$gonggao]);
        //修改公告后清空看过公告的人员
        $tangonggaoModel = new Tangonggao();
        $info = $tangonggaoModel->where(['qun_id'=>$qun_id])->find();
        if($info){
            $info =$info->toArray();
            $tangonggaoModel->where(['id'=>$info['id']])->delete();
        }
        $this->success('成功');
    }

    /**设置群头像**/
    public function setimg(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $img = $this->request->param('img');
        $userinfo =$this->auth->getUserinfo();
        $qunModel = new \app\admin\model\qun\Qun();
        $quninfo = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        if($quninfo['guanli']==''){
            $this->error('您不是群管理不能修改公告');
        }else{
            $idarr=explode(",",$quninfo['guanli']);
            if(!in_array($userinfo['id'],$idarr)){
                $this->error('您不是群管理不能修改公告');
            }
        }
        $qunModel->where(['id'=>$qun_id])->update(['img'=>$img]);
        $this->success('成功');
    }

    /**修改群名称**/
    public function settitle(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $name = $this->request->param('name');
        $userinfo =$this->auth->getUserinfo();
        $qunModel = new \app\admin\model\qun\Qun();
        $quninfo = $qunModel->where(['id'=>$qun_id])->find()->toArray();
        if($quninfo['guanli']==''){
            $this->error('您不是群管理不能修改公告');
        }else{
            $idarr=explode(",",$quninfo['guanli']);
            if(!in_array($userinfo['id'],$idarr)){
                $this->error('您不是群管理不能修改公告');
            }
        }
        $qunModel->where(['id'=>$qun_id])->update(['name'=>$name]);
        $this->success('成功');
    }

    /**关闭公告弹窗**/
    public function guanbi(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $userinfo =$this->auth->getUserinfo();
        $tangonggaoModel = new Tangonggao();
        $info = $tangonggaoModel->where(['qun_id'=>$qun_id])->find();
        if($info){
            $info =$info->toArray();
            if($info['userlist']==''){
                $tangonggaoModel->where(['id'=>$info['id']])->update(['userlist'=>$userinfo['id']]);
            }else{
                $arr=explode(",",$info['userlist']);
                if(!in_array($userinfo['id'],$arr)){
                    array_push($arr,$userinfo['id']);
                    $str=implode(",",$arr);
                    $tangonggaoModel->where(['id'=>$info['id']])->update(['userlist'=>$str]);
                }
            }
            $this->error('成功');
        }else{
            $indata['qun_id'] = $qun_id;
            $indata['userlist'] = $userinfo['id'];
            $indata['create_time'] = time();
            $tangonggaoModel->insert($indata);
            $this->error('成功');
        }
    }

    /**是否需要弹公告**/
    public function tan(){
        $qun_id = $this->request->param('qun_id');
        if(!$qun_id || $qun_id==''){
            $this->error('缺少群ID');
        }
        $userinfo =$this->auth->getUserinfo();
        $tangonggaoModel = new Tangonggao();
        $info = $tangonggaoModel->where(['qun_id'=>$qun_id])->find();
        if($info){
            $info = $info->toArray();
            if($info['userlist']==''){
                $this->success('成功',['is_tan'=>1]);
            }else{
                $arr=explode(",",$info['userlist']);
                if(in_array($userinfo['id'],$arr)){
                    $this->success('成功',['is_tan'=>0]);
                }else{
                    $this->success('成功',['is_tan'=>1]);
                }
            }
        }else{
            $this->success('成功',['is_tan'=>1]);
        }
    }
}